package com.itheima.map;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class MapDemo04 {
    public static void main(String[] args) {
        HashMap<Student,String> hashMap = new HashMap<>();
        hashMap.put(new Student("张三",23),"福建");
        hashMap.put(new Student("李四",18),"广东");
        hashMap.put(new Student("王五",30),"北京");

        Set<Map.Entry<Student, String>> entries = hashMap.entrySet();
        for (Map.Entry<Student, String> entry : entries) {
            System.out.println(entry.getKey()+"..."+entry.getValue());
        }
        System.out.println("---------------");

        //迭代器
        Iterator<Map.Entry<Student, String>> eIt = entries.iterator();
        while (eIt.hasNext()){
            Map.Entry<Student, String> next = eIt.next();
            System.out.println(next.getKey()+"+++"+next.getValue());
        }

        System.out.println("---------------");

        Set<Student> students = hashMap.keySet();
        for (Student stu : students) {
            System.out.println(stu +"===" +hashMap.get(stu));
        }

        System.out.println("---------------");

        //迭代器
        Iterator<Student> it = students.iterator();
        while (it.hasNext()){
            Student stu = it.next();
            System.out.println(stu + "***" +hashMap.get(stu));
        }

        System.out.println("---------------");

        hashMap.forEach(
                (Student key , String value)->{
                    System.out.println(key + "///" +value);
                }
        );


        System.out.println("---------------");
        System.out.println(hashMap);
    }
}

class Student{
    private String name;
    private Integer age;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Student student = (Student) o;

        if (name != null ? !name.equals(student.name) : student.name != null) return false;
        return age != null ? age.equals(student.age) : student.age == null;
    }

    @Override
    public int hashCode() {
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + (age != null ? age.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Student(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public Student() {
    }
}
